//
//  WBNewFeature.swift
//  Weibo
//
//  Created by 伏文东 on 2018/1/26.
//  Copyright © 2018年 伏文东. All rights reserved.
//

import UIKit

/// 新特性视图
class WBNewFeature: UIView {
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var enterButton: UIButton!
    @IBOutlet weak var pageControl: UIPageControl!
    
    @IBAction func enterStatus(_ sender: UIButton) {
        removeFromSuperview()
        
    }
    class func newFeature() -> WBNewFeature {
        let nView = Bundle.main.loadNibNamed("WBNewFeature", owner: nil, options: nil)?.first  as! WBNewFeature
        nView.frame = UIScreen.main.bounds
        return nView
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        let count = 4
        let rect = UIScreen.main.bounds
        for i in 0..<count {
            let imageName = "new_feature_\(i + 1)"
            let iv = UIImageView(image: UIImage(named: imageName))
            
            iv.frame = rect.offsetBy(dx: CGFloat(i) * rect.width, dy: 0)
            scrollView.addSubview(iv)
            
        }
        
        scrollView.contentSize = CGSize(width: CGFloat(count + 1) * rect.width, height: rect.height)
        scrollView.bounces = false
        scrollView.isPagingEnabled = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        enterButton.isHidden = true
        scrollView.delegate = self
    }
    
}

extension WBNewFeature: UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page == scrollView.subviews.count {
            removeFromSuperview()
        }
        enterButton.isHidden = (page != scrollView.subviews.count - 1)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        enterButton.isHidden = true
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        
        pageControl.currentPage = page
        
        pageControl.isHidden = (page == scrollView.subviews.count)
        
        
    }
}
























